Copper.TaskManager = (function(){
    var taskList = {};
    
    var data = [];
    
    var currentTaskId = null;
    
    var renderer;
    
    var dummyTask;
    
    var idSeed = 100;
    
    return {
        selectedTaskId: null,
        
        addTask: function(task){
            var date = Copper.Global.currentDate;
            task.startTime = date + ' ' + task.startTime;
            task.endTime = date + ' ' + task.endTime;
            task.id = ++idSeed;
            data.push(task);
            currentTaskId = task.id; 
            Copper.TaskManager.render(renderer);
        },
        
        updateTask: function(task){
            var date = Copper.Global.currentDate;
            task.startTime = date + ' ' + task.startTime;
            task.endTime = date + ' ' + task.endTime;
            for(var i = 0; i < data.length; ++i){
                if(data[i].id == task.id){
                    data[i] = task;
                    break;
                }
            }
            Copper.TaskManager.render(renderer);
        },
        
        removeTask: function(){},
        
        getCurrentTask: function(){
            return taskList[currentTaskId];
        },
        
        getCurrentTaskId: function(){
            return currentTaskId;
        },
        
        setCurrentTask: function(taskId){
        /*
            if(taskList[currentTaskId]){
                taskList[currentTaskId].unselect();
            }
            if(taskList[taskId]){
                taskList[taskId].select();
            }
            */
            currentTaskId = taskId;
        },
        
        isCurrentTask: function(taskId){
            return currentTaskId == taskId;
        },
        
        load: function(){
            //todo
            for(var i = 0; i < data.length; ++i){
                idSeed = Math.max(idSeed, data[i].id);
            }
        },
        
        render: function(layer){       
            layer.removeChildren();
            taskList = {};
  
            for(var i = 0; i < data.length; ++i){
                var o = data[i];
                var task = new Copper.Task({
                    id: o.id,
                    startTime: o.startTime.slice(9),
                    endTime: o.endTime.slice(9),
                    content: o.content,
                    renderTo: layer,
                    isSelected: currentTaskId == o.id
                });
                taskList[o.id] = task;
            }
            layer.draw();
            
            renderer = layer; 
        },
        
        setData: function(newData){
            data = newData;
        },
        
        refresh: function(){
            Copper.TaskManager.render(renderer);
        },
        
        isValidDroppingTarget: function(x, y){
            return true;//todo
        },
    
        showDummyTask: function(x, y){
            dummyLayer.removeChildren();
            dummyLayer.getCanvas().style.display = 'block';
                                                     
            var angle = Math.atan((Copper.Global.centerY - y) / (Copper.Global.centerX - x));
            if(Copper.Global.centerX > x){
                angle += Math.PI;
            }
            
           console.log('Angle:' + angle);
            var startAngle = angle - 15 / 720 * 2 * Math.PI;
            var endAngle = angle + 15 / 720 * 2 * Math.PI;
            console.log('startAngle:' + startAngle + ',endAngle:' + endAngle);
            
            var taskRing = new Copper.TaskRing({
                isSelected: true,
                startAngle: startAngle,
                endAngle: endAngle,
                fillStyle: 'white'
            });
            dummyLayer.add(taskRing);
            
            var taskBubble = new Copper.TaskBubble({
                isSelected: true,
                startAngle: startAngle,
                endAngle: endAngle,
                title: '',
                content: ''                             
            });
            dummyLayer.add(taskBubble);
            
            dummyLayer.draw();
        },
        
        showDummyTask2: function(task){
            dummyLayer.removeChildren();
            dummyLayer.show();
            dummyLayer.getCanvas().style.display = 'block';
            var dummyTask = new Copper.Task({

                startTime: task.startTime,
                endTime: task.endTime,
                content: task.content,
                renderTo: dummyLayer,
                isSelected: true
            });

            dummyLayer.draw();
        },
        
        hideDummyTask: function(){
            dummyLayer.hide();
            dummyLayer.getCanvas().style.display = 'none';
        },
        
        convertDummyTask: function(x, y, content){
            //x = 572; y=239
            var angle = Math.atan(Math.abs((Copper.Global.centerY - y) / (x - Copper.Global.centerX)));
            var time;
            if(Copper.Global.centerX < x && Copper.Global.centerY > y){
                time = 180 - angle / Math.PI * 2 * 180;
            } else if(Copper.Global.centerX < x && Copper.Global.centerY < y){
                time = 180 + angle / Math.PI * 2 * 180; 
            } else if(Copper.Global.centerX > x && Copper.Global.centerY < y){
                time = 540 - angle / Math.PI * 2 * 180; 
            } else {
                time = 540 + angle / Math.PI * 2 * 180; 
            }
            
            console.log('Angle:' + angle / Math.PI);
            
            var startTime = time - 15; 
            var endTime = time + 15;
            
            if(Copper.Global.amPm == 'PM'){
                 startTime += 720;
                 endTime += 720;
            }
            console.log('startTime:' + startTime + ', endTime:' + endTime);
            //var d = $.dateformat.format('D,M d', date)  
            
            var sh = Math.floor(startTime / 60);
            var sm = Math.round(startTime % 60);
            var eh = Math.floor(endTime / 60);
            var em = Math.round(endTime % 60);
             
              
            var task = {
                startTime: (sh < 10? ('0' + sh): sh) + ':' + (sm < 10? ('0' + sm): sm),
                endTime: (eh < 10? ('0' + eh): eh) + ':' + (em < 10? ('0' + em): em),
                content: content,
                color: ''
            };
               
            return task;
        },
        
        showTimeRegulator: function(task){
            dummyLayer.removeChildren();
            dummyLayer.show();
            dummyLayer.getCanvas().style.display = 'block';

            var regulator = new Copper.TimeRegulator({
                angle: task.startAngle
            });
            dummyLayer.add(regulator);
            regulator = new Copper.TimeRegulator({
                angle: task.endAngle
            });
            dummyLayer.add(regulator);

            dummyLayer.draw();        
        }
    };

})();